package untils

import (
	"fmt"
	"github.com/robfig/cron"
	"log"
	"order_srv/handler/models"
	"time"
)

func Cron() {
	c := cron.New()
	spec := "*/60 * * * * *"
	err := c.AddFunc(spec, func() {
		log.Println("时间戳", time.Now().Unix())
		var user models.User
		var UserCoupin models.UserCoupon
		ind, err := user.GetUserFInd()
		if err != nil {
			return
		}
		now := time.Now()
		for _, u := range ind {
			if now.Unix()-u.UserRegisterTime.Unix() > 7*3600*24 {
				UserCoupin.UserId = u.Id
				UserCoupin.CouponId = 6
				if err := UserCoupin.GetUserCouponInFo(u.Id); err != nil {
					fmt.Println("该用户查询失败", err)
					continue
				}

				fmt.Println("用户", u.Id)
				if UserCoupin.Id == 0 {
					fmt.Println("用户", u.Id, "已领取优惠券")
					if err := UserCoupin.UserCouponAdd(); err != nil {
						fmt.Println("优惠券添加失败", err)
						continue
					}
				}
			}
		}
	})
	fmt.Println(err)
	c.Start()
	select {}
}
